//
//  SRBaseViewController.m
//  YWQF_APP
//
//  Created by 孙锐 on 2018/3/22.
//  Copyright © 2018年 孙锐. All rights reserved.
//

#import "SRBaseViewController.h"

@interface SRBaseViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation SRBaseViewController{
    BOOL needShowBack;
    BOOL _needNavBar;
}


- (instancetype)initWithTitle:(NSString *)title needBack:(BOOL)needBack
{
    self = [super init];
    if (self) {
        needShowBack = needBack;
        self.title = title;
        _needNavBar = YES; // 默认显示自定义导航栏
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title needBack:(BOOL)needBack needNavBar:(BOOL)needNavBar
{
    self = [self initWithTitle:title needBack:needBack];
    if (self) {
        _needNavBar = needNavBar;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    [self doBaseSettings];
    if (_needNavBar) {
        [self.view addSubview:self.navBar];
        [self doNavigationBarSettings];
    }
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)doBaseSettings{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
}

-(void)doNavigationBarSettings{
    float statusBarHeight = kStatusBarHeight;
    float navBarHeight = 44+statusBarHeight;
    self.navBar.frame  = CGRectMake(0, 0, kScreenWidth, navBarHeight);
    [self.navBar setColor:UIColorFromRGB(0xFE777C) endColor:UIColorFromRGB(0xF2454E)];
    [self.navBar setBackgroundImage];
    [self.navBar setNavTitle:self.title];
    if (needShowBack) {
        __weak typeof(self) weakself = self;
        [self.navBar setBackBtnWithBlock:^{
            [weakself.navigationController popViewControllerAnimated:YES];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NavBar *)navBar{
    if (!_navBar) {
        _navBar = [[NavBar alloc]init];
    }
    return _navBar;
}

+ (void)resetDefaults {
    
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    
    NSDictionary* dict = [defs dictionaryRepresentation];
    
    for(id key in dict) {
        
        [defs removeObjectForKey:key];
        
    }
    
    [defs synchronize];
    
}


-(void)chooseImageWithoutEditing{
    UIAlertController *alertController;
    
    __block NSUInteger blockSourceType = 0;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        //支持访问相机与相册情况
        alertController = [UIAlertController alertControllerWithTitle:@"选择图片" message:@"请选择图片" preferredStyle:    UIAlertControllerStyleActionSheet];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing = NO;
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击拍照");
            //相机
            blockSourceType = UIImagePickerControllerSourceTypeCamera;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing =NO;//这两句保证了图片可以裁剪为正方形
            //            imagePickerController.allowsImageEditing=YES;//这两句保证了图片可以裁剪为正方形
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击取消");
            // 取消
            return;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        //只支持访问相册情况
        alertController = [UIAlertController alertControllerWithTitle:@"选择图片" message:@"请选择图片" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing =NO;//这两句保证了图片可以裁剪为正方形
            //            imagePickerController.allowsImageEditing=YES;//这两句保证了图片可以裁剪为正方形
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:^{
                
            }];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击取消");
            // 取消
            return;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}


-(void)chooseImage{
    UIAlertController *alertController;
    
    __block NSUInteger blockSourceType = 0;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        //支持访问相机与相册情况
        alertController = [UIAlertController alertControllerWithTitle:@"选择图片" message:@"请选择图片" preferredStyle:    UIAlertControllerStyleActionSheet];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing = YES;
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击拍照");
            //相机
            blockSourceType = UIImagePickerControllerSourceTypeCamera;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing =YES;//这两句保证了图片可以裁剪为正方形
//            imagePickerController.allowsImageEditing=YES;//这两句保证了图片可以裁剪为正方形
            
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击取消");
            // 取消
            return;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        //只支持访问相册情况
        alertController = [UIAlertController alertControllerWithTitle:@"选择图片" message:@"请选择图片" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            
            imagePickerController.allowsEditing =YES;//这两句保证了图片可以裁剪为正方形
//            imagePickerController.allowsImageEditing=YES;//这两句保证了图片可以裁剪为正方形
            imagePickerController.sourceType = blockSourceType;
            
            [self presentViewController:imagePickerController animated:YES completion:^{
                
            }];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击取消");
            // 取消
            return;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [self beginUpload];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (!image) {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];

//    [WebAgent uploadImageWithImage:image success:^(id responseObject) {
//        NSDictionary *dict = (NSDictionary *)responseObject;
//        NSString *imageUrl = dict[@"photourl"];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            NSLog(@"111");
//            [self getImage:image uploadUrl:imageUrl];
//        });
//    } failure:^(NSError *error) {
//        NSLog(@"%@", [error localizedDescription]);
//        NSLog(@"error");
//        [MBProgressHUD showError:[NSString stringWithFormat:@"网络异常，错误代码：%@",[error localizedDescription]]];
//        [self getImageFail];
//    }];
}

-(void)beginUpload{
    
}
-(void)getImageFail{
    
}

-(void)getImage:(UIImage *)image uploadUrl:(NSString *)url{
    
}

- (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}


//UIColorFromRGB(0xFE777C)
//UIColorFromRGB(0xF2454E)
-(void)setColor:(UIColor *)beginColor endColor:(UIColor *)endColor toView:(UIView *)view{
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = view.bounds;
    gradient.colors = [NSArray arrayWithObjects:
                       (id)beginColor.CGColor,
                       (id)endColor.CGColor,nil];
    gradient.startPoint = CGPointMake(0, 1);
    gradient.endPoint = CGPointMake(1, 1 );
    [view.layer addSublayer:gradient];
    
}

+ (NSString *)uuidString

{
    
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    
    CFRelease(uuid_ref);
    
    CFRelease(uuid_string_ref);
    
    NSString *string =[uuid lowercaseString];
    
    NSArray *arr = [string componentsSeparatedByString:@"-"];
    string = @"";
    for (NSString *subStr in arr) {
        
        string = [string stringByAppendingString:subStr];
    }
    
    return string;
    
}

+(NSString *)countNumAndChangeformat:(NSString *)number
{
    NSArray *subArr = [number componentsSeparatedByString:@"."];
    NSString *num = subArr[0];
    int count = 0;
    long long int a = num.longLongValue;
    while (a != 0)
    {
        count++;
        a /= 10;
    }
    NSMutableString *string = [NSMutableString stringWithString:num];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3) {
        count -= 3;
        NSRange rang = NSMakeRange(string.length - 3, 3);
        NSString *str = [string substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:@"," atIndex:0];
        [string deleteCharactersInRange:rang];
    }
    [newstring insertString:string atIndex:0];
    NSString *str = @"";
    if (subArr.count>1) {
       str  = [newstring stringByAppendingString:[NSString stringWithFormat:@".%@",subArr[1]]];
    }else{
        str = newstring;
    }
    return str;
}


@end
